package org.okkam.refine.strategy;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.okkam.client.data.AttributeMetadataType;
import org.okkam.client.data.AttributeType;
import org.okkam.client.data.AttributesType;
import org.okkam.client.data.Entity;
import org.okkam.client.data.ProfileType;
import org.okkam.client.util.XMLEntityConverter;
import org.okkam.refine.strategy.CandidateStrategy;
import org.okkam.refine.strategy.PersonCandidate;


public class PersonCandidate implements CandidateStrategy {

	/*
	 * A) Unsupervise positive: (perfect match) 
	 * 1) same fiscal code 
	 * 2) first  name, last name, place of birth, date of birth
	 * B) Unsupervise negative: (reject, remove from the candidates list)
	 *  different one of: fiscal code AND first name AND last name (all different)
	 */

	private static Log log = LogFactory.getLog(PersonCandidate.class);
	
	@Override
	public boolean checkEquality(List<AttributeType> attributeTypeList, List<String> entityList) {
	    	   
	    Iterator<AttributeType> attributeTypeIterator = attributeTypeList.iterator();
	    Map<String, String> propValueNewEntity = new HashMap<String, String>();
	    boolean match = false;
	    
            while (attributeTypeIterator.hasNext()) {
                AttributeType attributeType = attributeTypeIterator.next();
                String term = attributeType.getName().getLocalPart().toLowerCase();
                String value = attributeType.getValue().toLowerCase().replace("\"", "");
                AttributeMetadataType metadata = attributeType.getMetadata();
                propValueNewEntity.put(term, value);
        }            
            
            Iterator entityIterator = entityList.iterator();
            while (entityIterator.hasNext()) {
                    XMLEntityConverter converter = new XMLEntityConverter();
                    Entity entity = null;
                    try {
                        entity = converter.xmlToEntity( entityIterator.next().toString());
                    } catch (JAXBException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    ProfileType profile = entity.getProfile();
                    AttributesType attributesType = profile.getAttributes();
                    List<AttributeType> attributeTypeListResponse = attributesType.getAttributes();
                    Iterator<AttributeType> attributeTypeIteratorresponse = attributeTypeListResponse.iterator();

                    Map<String, String> propValueResponse = new HashMap<String, String>();

                    while (attributeTypeIteratorresponse.hasNext()) {
                            AttributeType attributeType = attributeTypeIteratorresponse.next();
                            String term = attributeType.getName().getLocalPart().toLowerCase();
                            String value = attributeType.getValue().toLowerCase().replace("\"", "");
                            AttributeMetadataType metadata = attributeType.getMetadata();
                            propValueResponse.put(term, value);
                    }
            		
            Iterator it = propValueNewEntity.keySet().iterator();
          
            //based on Fisical code
            if (propValueResponse.containsKey("codice_fiscale")) {

                while (it.hasNext()) {
                    String valOfQuery = propValueNewEntity.get(it.next()).replace("\"", "");
                    if (valOfQuery.equals(propValueResponse.get("codice_fiscale").toLowerCase())) {
                        match = true;
                        return match;
                    }
                }

            }
	 //based on first name, last name,  ...            
            if (propValueResponse.containsKey("first_name") 
                    && propValueResponse.containsKey("last_name")
                    && propValueResponse.containsKey("birthdate") 
                    && propValueResponse.containsKey("birthplace")) {

                ArrayList<String> responseNameChecks = new ArrayList<String>();

                responseNameChecks.add(propValueResponse.get("first_name"));
                responseNameChecks.add(propValueResponse.get("last_name"));
                responseNameChecks.add(propValueResponse.get("birthdate"));
                responseNameChecks.add(propValueResponse.get("birthplace"));

                Iterator itNameCheck = responseNameChecks.iterator();

                while (itNameCheck.hasNext()) {
                    match=false;
                    String valOfResponse = (String) itNameCheck.next();
                    
                    ArrayList<String> newNameChecks = new ArrayList<String>();
                    newNameChecks.add(propValueNewEntity.get("first_name").replace("\"", ""));
                    newNameChecks.add(propValueNewEntity.get("last_name").replace("\"", ""));
                    newNameChecks.add(propValueNewEntity.get("birthdate").replace("\"", ""));
                    newNameChecks.add(propValueNewEntity.get("birthplace").replace("\"", ""));
                    
                    
                    Iterator itq =newNameChecks.iterator();

                    while (itq.hasNext()) {
                        String valOfNewEntity = itq.next().toString();
                        if (valOfResponse.toLowerCase().equals(valOfNewEntity.toLowerCase())) {
                            match = true;
                            break;
                        }
                    }
                    if (!match) {
                        break;
                    }
                }
                if(match){
                    return match;
                }
            }
	}
            return match;
	}
}